const Joi = require('joi');
const { objectId } = require('./custom.validator'); // 假设您有自定义的objectId校验器

const foodItemSchema = Joi.object({
  food: Joi.object({
    _id: Joi.custom(objectId).required(),
    name: Joi.string().required(),
    image: Joi.string().uri().allow('', null),
    unit: Joi.string(), // 食物本身单位，创建时可选，可能从food库带出
    calories: Joi.number(), // 食物本身单位热量，创建时可选
  }).required(),
  amount: Joi.number().min(0).required(),
  unit: Joi.string().required(),
  calories: Joi.number().min(0).required(), // 实际摄入热量
  nutrition: Joi.object({
    protein: Joi.number().min(0).default(0),
    fat: Joi.number().min(0).default(0),
    carbohydrate: Joi.number().min(0).default(0),
    // 根据模型扩展其他营养素
  }).default({ protein: 0, fat: 0, carbohydrate: 0 }),
});

const createDietRecord = {
  body: Joi.object().keys({
    userId: Joi.custom(objectId).required(), // 在实际应用中，通常从req.user获取，此处为方便测试或特定场景
    mealType: Joi.string().valid('早餐', '午餐', '晚餐', '加餐').required(),
    date: Joi.date().iso().required(),
    time: Joi.string().regex(/^([01]\d|2[0-3]):([0-5]\d)$/).required(), // HH:mm format
    foods: Joi.array().items(foodItemSchema).min(1).required(),
    note: Joi.string().allow('', null),
    images: Joi.array().items(Joi.string().uri()).allow(null),
    // totalCalories 和 totalNutrition 由mongoose中间件计算，不需要前端传递
  }),
};

const getDietRecords = {
  query: Joi.object().keys({
    userId: Joi.custom(objectId),
    mealType: Joi.string().valid('早餐', '午餐', '晚餐', '加餐'),
    startDate: Joi.date().iso(),
    endDate: Joi.date().iso().min(Joi.ref('startDate')),
    minTotalCalories: Joi.number().min(0),
    maxTotalCalories: Joi.number().min(Joi.ref('minTotalCalories')),
    sortBy: Joi.string(), // e.g., 'date:desc,totalCalories:asc'
    limit: Joi.number().integer().min(1),
    page: Joi.number().integer().min(1),
    search: Joi.string().allow(''), // 用于模糊搜索食物名称或备注等
  }),
};

const getDietRecord = {
  params: Joi.object().keys({
    id: Joi.custom(objectId).required(),
  }),
};

const updateDietRecord = {
  params: Joi.object().keys({
    id: Joi.custom(objectId).required(),
  }),
  body: Joi.object()
    .keys({
      mealType: Joi.string().valid('早餐', '午餐', '晚餐', '加餐'),
      date: Joi.date().iso(),
      time: Joi.string().regex(/^([01]\d|2[0-3]):([0-5]\d)$/), // HH:mm format
      foods: Joi.array().items(foodItemSchema).min(1),
      note: Joi.string().allow('', null),
      images: Joi.array().items(Joi.string().uri()).allow(null),
      // userId 不允许在此处更新
      // totalCalories 和 totalNutrition 由mongoose中间件计算
    })
    .min(1), // 确保至少有一个字段需要更新
};

const deleteDietRecord = {
  params: Joi.object().keys({
    id: Joi.custom(objectId).required(),
  }),
};

// 统计摘要接口的校验，可以根据实际需要的参数添加
const getDietRecordSummary = {
  query: Joi.object().keys({
    userId: Joi.custom(objectId), // 可选，如果为特定用户统计
    date: Joi.date().iso(), // 可选，默认为今天
    period: Joi.string().valid('day', 'week', 'month').default('week'), // 统计周期
  }),
};


module.exports = {
  createDietRecord,
  getDietRecords,
  getDietRecord,
  updateDietRecord,
  deleteDietRecord,
  getDietRecordSummary,
}; 